package phone.qyg.com.qyg.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.OptionImageUtils;
import com.app.utils.PromptManager;
import com.app.view.ItemImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.ActivityDetail;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.model.RegisUser;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/9 10:20
 * 邮箱：lanbuhan@163.com
 * 功能：活动报名
 */
@ContentView(R.layout.act_regis)
public class ActivityRegisActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.item_img)
    ItemImageView item_img;

    @ViewInject(R.id.title)
    TextView title;

    @ViewInject(R.id.time)
    TextView time;

    @ViewInject(R.id.regis_end_time)
    TextView regis_end_time;

    @ViewInject(R.id.map)
    TextView map;

    @ViewInject(R.id.submit)
    TextView submit;

    @ViewInject(R.id.regis_name)
    EditText regis_name;

    @ViewInject(R.id.regis_phone)
    EditText regis_phone;

    @ViewInject(R.id.regis_address)
    EditText regis_address;

    @ViewInject(R.id.regis_remark)
    EditText regis_remark;

    ActivityDetail actDetail;

    int id;

    @Override
    public void initSucessView() {
        Object obj = getTransModels();

        if(obj instanceof ActivityDetail){
            ActivityDetail actDetail = (ActivityDetail) obj;
            id = actDetail.id;
        }else if(obj instanceof Base){
            Base base = (Base) obj;
            id = base.id;
        }


    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(0 == id)return;
        mapKeys.put(AppHttpKey.ACT_ID,String.valueOf(id));
        AppHttpRequest.httpRequestDefalut(ApplicationConfig.ACT_DETAIL,
                mapKeys,
                new AppHttpRequest.OnAppHttpListener() {
                    @Override
                    public void onAppHttpState(boolean isState, String result) {
                        if(isState){
                            if(JSONTool.isStatus(result)){
                                actDetail = JSONTool.jsonDefaluTranClazz(result,null, ActivityDetail.class);
                                setData();
                            }
                        }
                    }
                });
    }


    private void setData(){
        if(null == actDetail) return;
        time.setText(setAttributeText(actDetail.startTime)+" ~ "+ setAttributeText(actDetail.endTime));
        regis_end_time.setText(setAttributeText(actDetail.signEndTime));
        map.setText(setAttributeText(actDetail.address));
        title.setText(setAttributeText(actDetail.title));
        back.setText(setAttributeText(actDetail.title));
        setImgPath(actDetail.fixImgUrl,item_img, OptionImageUtils.getRectRatioAct());
    }

    @Event(value = {R.id.back,R.id.submit})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.submit:

                String name = getEditToString(regis_name);
                String phone = getEditToString(regis_phone);
                String address = getEditToString(regis_address);
                final String remark = getEditToString(regis_remark);

                if(!isRequestStr(name)){
                    PromptManager.showToast(R.string.please_regis_name);
                    return;
                }

                if(!isRequestStr(phone)){
                    PromptManager.showToast(R.string.please_regis_phone);
                    return;
                }

                if(!CommonUtil.judgePhoneNums(phone))
                    return;

                RegisUser regisUser = new RegisUser(name,phone,address,remark);

                String dataValue = JSON.toJSONString(regisUser);

                mapKeys.put(AppHttpKey.ACT_ID,String.valueOf(actDetail.id));
                mapKeys.put(AppHttpKey.DATA,dataValue);

                AppHttpRequest.httpPostRequestDefalut(ApplicationConfig.SUB_REGIST_INFO,
                        mapKeys,
                        new AppHttpRequest.OnAppHttpListener() {
                            @Override
                            public void onAppHttpState(boolean isState, String result) {
                                    if(isState){
                                        PromptManager.showToast(JSONTool.errorHint(result));
                                        if(JSONTool.isStatus(result)){
                                            ActivityRegisActivity.this.finish();
                                        }
                                    }
                            }
                });
                break;
        }
    }

    @Override
    public void initViewHandler() {
        regis_name.addTextChangedListener(this);
        regis_phone.addTextChangedListener(this);
        regis_address.addTextChangedListener(this);
        regis_remark.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        submit.setEnabled(false);
        submit.setBackgroundResource(R.color.divider_two);
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(isEmptyOrHint(regis_name,R.string.please_user) &&
                isEmptyOrHint(regis_phone,R.string.please_phone_act)){
            submit.setEnabled(true);
            submit.setBackgroundResource(R.color.login_fill_btn);
        }else{
            submit.setEnabled(false);
            submit.setBackgroundResource(R.color.divider_two);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
